/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edi.controller;

import edi.api.Errors;
import edi.api.request.LoginRequest;
import edi.api.response.LoginResponse;
import edi.dao.UserDao;
import edi.entities.UserEntity;
import edi.entities.DepartmentUserEntity;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/login")
public class LoginController {

    private final static Logger LOGGER = Logger.getLogger(LoginController.class.getName()); 
    @Autowired
    UserDao userDAO;

    @ResponseBody
    @RequestMapping(value = "/authenticate", method = RequestMethod.POST)
    public LoginResponse loginUser(@RequestBody LoginRequest params) {
        
        LOGGER.info(params.getUsername());
        LOGGER.info(params.getPasswordHash());
        
        LoginResponse response = new LoginResponse();

        UserEntity userEntity = userDAO.findByLogin(params.getUsername());
        if(userEntity != null ){
            if(userEntity.getUsrPassword().equals(params.getPasswordHash())){
                response.setFirstName(userEntity.getUsrFirstName());
                response.setLastName(userEntity.getUsrLastName());
                response.setUserId(userEntity.getId().toString());
                response.setLogin(userEntity.getUsrLogin());
                response.setDepId(userEntity.getUserDeps().get(0).getUdeDepartment().getDepId());
                response.setErrorCode(Errors.NO_ERROR);
                return response;
            }
//        LOGGER.info(userEntity.getUserDeps().get(0).toString());
//        if (userEntity != null || !userEntity.getUsrPassword().equals(params.getPasswordHash())) {
//            response.setErrorCode(Errors.GENERAL_ERROR);
//            String errorMessage = "Unsuccessful login try for: " + userEntity.getUsrFirstName()
//                    + " " + userEntity.getUsrLastName()
//                    + ", id: " + userEntity.getUsrId()
//                    + ", department: " + userEntity.getUserDeps().get(0);
//            response.setErrorDesc(errorMessage);
//            return response;
//        }
//        response.setErrorCode(Errors.NO_ERROR);
//        String message = "Login successful!";
//        response.setErrorDesc(message);
            
//        DepartmentUserEntity depEntity = userEntity.getUserDeps().get(0);
            response.setErrorCode(Errors.GENERAL_ERROR);
            return response;
        }
        
        response.setErrorCode(Errors.MISSING_PARAM);
        return response;
    }
}
